package com.financing.wap.web.controller.api;

import java.sql.SQLException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.financing.wap.common.ResultMap;
import com.financing.wap.common.Session;
import com.financing.wap.common.SessionUtil;
import com.financing.wap.common.ResultMap.STATUS;
import com.financing.wap.dto.UserDTO;
import com.financing.wap.dto.UserFeedBackDTO;
import com.financing.wap.service.IUserFeedBackService;
import com.financing.wap.web.controller.BaseController;
import com.financing.wap.util.Constants;
import com.financing.wap.util.DTOdateSetUtil;

@Controller("userFeedBackController")
@RequestMapping("/api/user")
public class UserFeedBackController extends BaseController  { 

	Logger logger = Logger.getLogger(getClass());

	@Autowired
	private  IUserFeedBackService  userFeedBackServiceImpl;
	
	/**
	 * 交易详情
	 * @return
	 */
	@RequestMapping("/userFeedBack")
	@ResponseBody
	public ResultMap view(HttpServletRequest request, HttpServletResponse response,UserFeedBackDTO userFeedBackDTO,String _pc){ 
		
		ResultMap result = new ResultMap();
		try {
			Session session = SessionUtil.getSession();
			
			if(StringUtils.isNotBlank(_pc)){
				String verifyCode = (String)session.getAttribute(Constants.SESSION_KEY_VERIFYCODE);
				String requestVerifyCode = request.getParameter("verifyCode");
				SessionUtil.getSession().setAttribute(Constants.SESSION_KEY_VERIFYCODE, null);
				log.info("verifyCode is :"+verifyCode);
				log.info("requestVerifyCode is :"+requestVerifyCode);
				if(StringUtils.isEmpty(verifyCode)||!verifyCode.equals(requestVerifyCode.toLowerCase())){
					result.setStatus(STATUS.FAILE);
					result.setStatusCode(ResultMap.LOGIN_VERIFYCODE_ERROR);
					result.setMessage("图片验证码错误");
					return result;
				}
			}
			
			UserDTO loginUser = session.getUser();
			userFeedBackDTO.setFpUserMobile(loginUser.getMobile());
			DTOdateSetUtil.setCreateInfo(userFeedBackDTO);
			DTOdateSetUtil.setUpdateInfo(userFeedBackDTO);
			userFeedBackServiceImpl.addUserFeedBack(userFeedBackDTO);
			result.setStatus(STATUS.SUCCESS);
			result.setStatusCode(ResultMap.STATUS_CODE_SUCCESS);
		} catch (SQLException e) {
			log.error("add feed back error!",e);
			result.setStatus(STATUS.ERROR);
			result.setStatusCode(ResultMap.STATUS_CODE_SYS_EXCEPTION);
		}
		
		return result;
	}

}
